package org.jeecg.modules.CheckPlan.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.utils.UuidUtils;
import org.jeecg.modules.CheckPlan.entity.CheckPlan;
import org.jeecg.modules.CheckPlan.mapper.CheckPlanMapper;
import org.jeecg.modules.CheckPlan.service.ICheckPlanService;
import org.jeecg.modules.CheckTask.entity.CheckTask;
import org.jeecg.modules.CheckTask.entity.CheckTaskDetail;
import org.jeecg.modules.CheckTask.mapper.CheckTaskDetailMapper;
import org.jeecg.modules.CheckTask.mapper.CheckTaskMapper;
import org.jeecg.modules.Safe.entity.DangerList;
import org.jeecg.modules.Safe.mapper.DangerListMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Description: 排查计划表
 * @Author: jeecg-boot
 * @Date:   2023-02-19
 * @Version: V1.0
 */
@Slf4j
@Service
public class CheckPlanServiceImpl extends ServiceImpl<CheckPlanMapper, CheckPlan> implements ICheckPlanService {

    @Autowired
    private CheckPlanMapper checkPlanMapper;
    @Autowired
    private CheckTaskMapper checkTaskMapper;
    @Autowired
    private CheckTaskDetailMapper checkTaskDetailMapper;
    @Autowired
    private DangerListMapper dangerListMapper;

    @Override
    public void generateCheckTask() {
        List<CheckPlan> checkPlans = listAllCheckPlan();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (CheckPlan checkPlan :
                checkPlans) {
            String executionTime = dateFormat.format(checkPlan.getNextDate()) ;
            Calendar calendar = Calendar.getInstance();
            Date parse = null;
            try {
                parse = dateFormat.parse(executionTime);
                //获取当前日期时注释掉
                calendar.setTime(parse);
                parse = calendar.getTime();

                if (dateFormat.format(new Date()).equals(dateFormat.format(parse))) {
                    //生成排查任务

                    String taskId = UuidUtils.getUUID();
                    CheckTask checkTask = new CheckTask();
                    BeanUtils.copyProperties(checkPlan,checkTask);
                    checkTask.setState("0");
                    checkTask.setPlanCode(checkPlan.getPlanCode());
                    checkTask.setPlanDate(checkPlan.getNextDate());
                    checkTask.setCreateTime(new Date());
                    checkTask.setCreateBy("sys");
                    checkTask.setPlanName(checkPlan.getPlanName());
                    checkTask.setRegionName(checkPlan.getRegionName());

                    checkTask.setId(taskId);
                    checkTaskMapper.insert(checkTask);


                    String[] dangerIds = checkPlan.getDangerName().split(",");

                    //生成排查任务明细
                    for (String dangerId:
                            dangerIds) {
                        CheckTaskDetail checkTaskDetail = new CheckTaskDetail();
                        DangerList dangerList = dangerListMapper.selectById(dangerId);
                        BeanUtils.copyProperties(dangerList,checkTaskDetail);
                        checkTaskDetail.setTaskId(taskId);
                        checkTaskDetail.setId(UuidUtils.getUUID());
                        checkTaskDetail.setCheckTool(dangerList.getCheckTool());
                        checkTaskDetail.setDamageDegree(dangerList.getDamageDegree());
                        checkTaskDetail.setDangerGrade(dangerList.getDangerGrade());
                        checkTaskDetail.setIssueType(dangerList.getIssueType());
                        checkTaskDetail.setSafeCriterion(dangerList.getSafeCriterion());
                        checkTaskDetail.setDangerProjectcode(dangerList.getDangerProjectcode());
                        checkTaskDetail.setDangerProjectname(dangerList.getDangerProjectname());
                        checkTaskDetail.setCreateTime(new Date());
                        checkTaskDetail.setRegionName(checkPlan.getRegionName());
                        checkTaskDetail.setUpdateTime(new Date());
                        checkTaskDetailMapper.insert(checkTaskDetail);
                    }

                    //更新排查计划下次执行时间
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(checkPlan.getNextDate());
                    cal.add(Calendar.DATE, checkPlan.getExecutionInterval());
                    checkPlan.setNextDate(cal.getTime());
                    checkPlanMapper.updateById(checkPlan);


//                    CheckTask checkTask = new CheckTask();
//                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//                    BeanUtils.copyProperties(checkPlan,checkTask);
//                    try {
//                        checkTask.setPlanId(checkPlan.getId());
//                        checkTask.setRegionId(checkPlan.getRegionId());
//                        checkTask.setPlanDate(dateFormat.parse(checkPlan.getExecutionTime()));
//                    } catch (ParseException e) {
//                        e.printStackTrace();
//                    }
//                    String id = snowFlake.nextStringId();
//                    List<DangerLink> linkList = dangerLinkDao.getDangerLinkeByPlanId(checkPlan.getId());
//                    for (DangerLink link: linkList) {
//                        link.setTaskId(id);
//                        link.setId(snowFlake.nextStringId());
//                        link.setRegionId(checkPlan.getRegionId());
//                        BaseUtils.setDefaultFiledsBaseModel(link,true);
//                        dangerLinkDao.insert(link);
//                    }
//                    checkTask.setState("1");
//                    checkTask.setId(id);
//                    checkTask.setRemark(checkPlan.getRemark());
//                    checkTask.setCreateDate(new Date());
//
//                    checkTaskDao.insert(checkTask);
//
//                    HashMap<String, List<String>> weiXinMap = checkPlanService.listWeiXinCode( checkPlan.getRegionId());
//                    String content=checkPlan.getPlanName()+" ("+checkPlan.getPlanCode()+") 任务已经生成，备注："+checkPlan.getRemark()+"，请按时执行。";
//                    WeiXinUtil.WeiXinSend(weiXinMap.get("weiXinCodes"),content);
//                    pdaMessageMngService.generatePdaMessage(weiXinMap.get("userName"),content);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

    /**
     * 带出全部隐患排查计划
     * @return
     */
    public List<CheckPlan> listAllCheckPlan() {
        HashMap<String, Object> queryMap = new HashMap<>();
        queryMap.put("state", "0");
        List<CheckPlan> checkPlans = checkPlanMapper.selectByMap(queryMap);
        return checkPlans;
    }
}
